package com.woniuxy.classdeans.controller;

import com.woniuxy.classdeans.exception.ClassdeansException;
import com.woniuxy.classdeans.service.GradeInfoService;
import com.woniuxy.commons.entity.ResponseResult;
import com.woniuxy.commons.entity.wyh.GradeInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.List;

/**
 * @author 王艳辉
 * @date 2020/7/21
 */
@RestController
@RequestMapping("gradeInfo")
@Slf4j
@Transactional
public class GradeInfoController {

    @Autowired
    private GradeInfoService gradeInfoService;

    /**
     * 获取今年的所有年级信息
     * @return
     */
    @RequestMapping("getall")
    public Object getAll(){

        //返回消息类型默认失败
        ResponseResult<List<GradeInfo>> res = new ResponseResult<List<GradeInfo>>().setCode(400).setMessage("获取信息失败！");

        try {
            Calendar date = Calendar.getInstance();
            String year = String.valueOf(date.get(Calendar.YEAR));

            List<GradeInfo> all = gradeInfoService.getAll(year);
            //获取成功
            if(all!=null){
                res.setMessage("成功").setCode(200).setData(all);
            }
        }catch (Exception e) {
            log.error(e.getMessage());//日志记录
            //抛自定义异常
            throw new ClassdeansException("gradeInfo/getall异常");
        }

        return res;
    }

}
